<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  const animationDuration = 4000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      barOption: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      'barOption': {
        deep: true,
        handler: function() {
          this.initChart()
        }
      }
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          title: {
            text: '近7天销售额统计',
            top: '1%',
            left: '3%',
            textStyle: {
              color: "#2d384b",
              fontSize: 18,
              fontWeight: 600
            }
          },
          tooltip: {
            show: false,
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: '8%',
            left: '1%',
            right: '1%',
            bottom: '2%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: this.barOption.xDate,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#2d384b'
              }
            },
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#2d384b'
              }
            },
            axisLabel: {
              interval: 0,
              margin: 8,
              color: '#2d384b',
              textStyle: {
                fontSize: 10
              },
            },
          }],
          series: [{
            name: 'pageA',
            type: 'bar',
            stack: 'vistors',
            barWidth: '50%',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#576f99' // 0% 处的颜色
                }, {
                  offset: 1,
                  color: '#2d384b' // 100% 处的颜色
                }], false),
                shadowColor: 'rgba(243, 160, 116,1)',
                shadowBlur: 0,
              }
            },
            data: this.barOption.yDate,
            animationDuration,
            label: {
              normal: {
                show: true,
                lineHeight: 18,
                formatter: '{c} 元',
                position: 'top',
                textStyle: {
                  color: '#000000',
                  fontSize: 12
                }
              }
            }
          }]
        })
      }
    }
  }
</script>
